package com.ruoyi.framework.mqtt.handler;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.project.instrument.domain.InsInstrument;
import com.ruoyi.project.instrument.domain.InsInstrumentUseStatistics;
import com.ruoyi.project.instrument.domain.InsMaintainLog;
import com.ruoyi.project.instrument.service.IInsInstrumentService;
import com.ruoyi.project.instrument.service.IInsInstrumentUseStatisticsService;
import com.ruoyi.project.instrument.service.IInsMaintainLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Taney
 */
@Component
public class InstrumentMaintainHandler extends AbstractHandler {

    public static final String TOPIC = "/event/deviceMaintain";

    @Autowired
    IInsMaintainLogService maintainLogService;

    @Override
    public void handleMessage(Message message, String topic, String msg) throws MessagingException {
        if (matches(TOPIC, topic)) {
            ClientInfo clientInfo = getClientInfo(topic);
            String product = clientInfo.getProduct();
            String deviceId = clientInfo.getDeviceId();
            logger.debug("设备[{}-{}]维护日志【{}】", product, deviceId, msg);
            /*
            {
            "userid":"",//维护人id
            "username":"",//维护人姓名
            "maintainTime":100000000,//维护时间=>时间戳（毫秒）,
            "maintainType":1,//维护类型1.日维护2.周维护3.月维护
            }
             */
            if (isJson(msg)) {
                //保存维护记录
                InsMaintainLog insMaintainLog = JSONUtil.toBean(msg, InsMaintainLog.class);
                maintainLogService.save(insMaintainLog);
                reStatistics(deviceId, MAINTAIN,null);
            }
        }
    }


}
